package com.ciwong.epaper.modules.epaper.ui.newlistenspeak.shortcoversation.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.dbbean.LswAnswer;
import com.ciwong.epaper.modules.epaper.bean.Attachments;
import com.ciwong.epaper.modules.epaper.bean.OptionDetail;
import com.ciwong.epaper.modules.epaper.bean.OptionInfo;
import com.ciwong.epaper.modules.epaper.bean.Questions;
import com.ciwong.epaper.util.ESystem;
import com.ciwong.mobilelib.utils.BaseJumpManager;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import static android.view.Gravity.CENTER;
import static android.view.Gravity.CENTER_VERTICAL;

/**
 * date :2018/5/3
 * author: loco
 * description: 选择题每个小题的 小标题及选项
 * version:
 * email: lzh838330255@163.com
 */
public abstract class BaseOptionsLayout extends LinearLayout {

    private Context context;
    private LinearLayout singleOptionll;//单个选项item
    private View viewOptionContent;//每个选项的内容
    //    private TextView viewOptionHeads;//选项前面的英文表示”A"“B”等
    protected String[] optionHeads = new String[]{"A", "B", "C", "D", "E", "F",
            "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
            "T", "U", "V", "W", "X", "Y", "Z"};
    private TextView viewOptionHeads;
    private IClickForAnswer iClickForAnswer;
    protected int SIZE_0 = 0;
    protected int SIZE_1 = 1;
    protected int SIZE_6 = 6;
    protected int SIZE_10 = 10;
    protected int SIZE_12 = 12;
    protected int SIZE_15 = 15;
    protected int SIZE_16 = 16;
    protected int SIZE_18 = 18;
    protected int SIZE_21 = 21;
    protected int SIZE_25 = 25;
    protected int SIZE_28 = 28;
    protected int SIZE_51 = 51;
    protected int SIZE_60 = 60;
    protected int SIZE_116 = 116;
    protected int SIZE_145 = 145;

    public interface IClickForAnswer {
        void clickForAnswer(LswAnswer lswAnswer, int position);
    }

    public void setIClickForAnswer(IClickForAnswer iClickForAnswer) {
        this.iClickForAnswer = iClickForAnswer;

    }

    public BaseOptionsLayout(Context context) {
        this(context, null);
    }


    public BaseOptionsLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public BaseOptionsLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        this.setOrientation(VERTICAL);
    }

    /**
     * 加载选项布局
     *
     * @param question      每个小选择题
     * @param isSingleCheck 是单选还是多选   @return
     */
    public void loadOptionLayout(final Questions question, final boolean isSingleCheck) {

        if (question == null || question.getOptions() == null || question.getOptions().size() < 1)
            return;
        final List<OptionInfo> optionInfoList = question.getOptions();
        removeAllViews();
        addView(loadOptionTittleLayout(question, context), 0);//添加小题标题
        final LswAnswer lswAnswer = question.getLswAnswer();//此小题的答案
        //创建选项
        for (int i = 0; i < optionInfoList.size(); i++) {
            final OptionInfo optionInfo = optionInfoList.get(i);
            //初始化选时可以加载选项是否是选中状态
            if (lswAnswer.getAnswerContents() != null && lswAnswer.getAnswerContents().size() > 0)
                if (optionInfo.getId().equals( lswAnswer.getAnswerContents().get(0).getContent()))
                    optionInfo.setCheckedAnswer(true);
            //单个选项item
            singleOptionll = new LinearLayout(context);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//            layoutParams.bottomMargin=dip2px(context, SIZE_21);
            singleOptionll.setLayoutParams(layoutParams);
            singleOptionll.setPadding(dip2px(context, SIZE_15),dip2px(context, SIZE_10),dip2px(context, SIZE_0),dip2px(context, SIZE_10));
            singleOptionll.setOrientation(HORIZONTAL);
            singleOptionll.setGravity(CENTER_VERTICAL);
            singleOptionll.setBackground(getResources().getDrawable(R.drawable.selector_listen_speak_item_bg_selector));
            singleOptionll.setSelected(optionInfo.isCheckedAnswer());
            //选项前面的英文表示”A"“B”等
            viewOptionHeads = new TextView(context);
            viewOptionHeads.setBackground(getResources().getDrawable(R.drawable.selector_listen_speak_option_header_bg));
            viewOptionHeads.setLayoutParams(new LayoutParams(dip2px(context, SIZE_25), dip2px(context, SIZE_25)));
            viewOptionHeads.setGravity(CENTER);
            viewOptionHeads.setTextColor(getResources().getColorStateList(R.color.check_textview_option));
            viewOptionHeads.setText(optionHeads[i]);
            viewOptionHeads.setTextSize(SIZE_12);
            viewOptionHeads.setSelected(optionInfo.isCheckedAnswer());
            //选项内容(是文字或者图片)
            viewOptionContent = singleOptionContent(optionInfo, context);
            //生成每个选项
            singleOptionll.addView(viewOptionHeads);
            singleOptionll.addView(viewOptionContent);

            final int finalI = i;
            singleOptionll.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //设置某个选项被选中
                    optionInfo.setCheckedAnswer(!optionInfo.isCheckedAnswer()); //存放选中状态;
                    changeBackground(isSingleCheck, finalI, optionInfoList, lswAnswer);
                }
            });
            addView(singleOptionll);
        }
    }


    /**
     * 选项内容(是文字或者图片)
     *
     * @param optionInfo 单个选项信息
     * @param context    为单个选项创建的容器
     * @return
     */
    protected abstract View singleOptionContent(OptionInfo optionInfo, Context context);

    /**
     * 加载选项的标题
     *
     * @param question 单个小题信息
     * @param context  上下文
     * @return 返回标题view
     */
    protected abstract View loadOptionTittleLayout(Questions question, Context context);

    /**
     * 选项点击后 改变背景色
     *
     * @param isSingleCheck 是单选还是多选
     * @param position      点击的条目
     * @param optionList    选择题备选答案集合
     * @param lswAnswer     选择题答案
     */

    private void changeBackground(boolean isSingleCheck, int position, List<OptionInfo> optionList, LswAnswer lswAnswer) {
        int differNum = getChildCount() - optionList.size();//看是否有选项以外的条目存在
        int inViewPosition = position + differNum;
        if (getChildAt(inViewPosition) instanceof View && !(getChildAt(inViewPosition) instanceof ViewGroup))
            return;
        ViewGroup clickedItem = (ViewGroup) getChildAt(position + differNum);
        View clickedheader = clickedItem.getChildAt(0);
        //若是单选选清除选中，再选中对应项；若是多选则不用过多处理，只需根据点击确定条目选中与否自己
        if (isSingleCheck) {
            for (int i = differNum; i < getChildCount(); i++) {
                if (getChildAt(i) instanceof View && !(getChildAt(i) instanceof ViewGroup))
                    continue;
                ViewGroup childLL = (ViewGroup) getChildAt(i);
                View childHeader = childLL.getChildAt(0);
                if ((i - differNum) == position) continue;
                childHeader.setSelected(false);
                childLL.setSelected(false);
                optionList.get(i - differNum).setCheckedAnswer(false);
            }
        }
        clickedheader.setSelected(optionList.get(position).isCheckedAnswer());
        clickedItem.setSelected(optionList.get(position).isCheckedAnswer());

        if (lswAnswer == null) return;
        //改变答案状态和内容
        if (optionList.get(position).isCheckedAnswer()) {//被选中
            lswAnswer.setAnswer(true);
            lswAnswer.getAnswerContents().get(0).setContent(optionList.get(position).getId());
        } else {//未被选中
            lswAnswer.setAnswer(false);
            lswAnswer.getAnswerContents().get(0).setContent("");
        }
        if (iClickForAnswer != null) {
            iClickForAnswer.clickForAnswer(lswAnswer, position);
        }
    }

    public LinearLayout getSingleOptionll() {
        return singleOptionll;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
